﻿using Dapper;
using lwzlz.Common;
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace lwzlz.Core.Repository
{
    /// <summary>
    /// 默认仓储接口实现局部类
    /// </summary>
    public partial class Repository<TEntity> : IRepository<TEntity> where TEntity : ModelBase, new()
    {
        protected IDbConnection dbconn;
        public string ModuleName { get; set; }

        public Repository()
        {
            ModuleName = AttributeHelper.GetModuleAttribute(this.GetType());
            if (dbconn == null)
            {
                dbconn = this.CreateDbConnection();
            }
        }

        /// <summary>
        /// 获取数据库连接
        /// </summary>
        /// <returns></returns>
        public SqlConnection CreateDbConnection()
        {
            return DatabaseHelper.GetSqlConnection(ModuleName);
        }

        /// <summary>
        /// 开始数据库事务
        /// </summary>
        /// <returns></returns>
        public IDbTransaction BeginTransaction()
        {
            return dbconn.BeginTransaction();
        }

        #region 执行SQL

        public int Execute(string sql, object param = null, IDbTransaction transaction = null,
            int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?))
        {
            return dbconn.Execute(sql, param, transaction, commandTimeout, commandType);
        }

        public Task<int> ExecuteAsync(string sql, object param = null, IDbTransaction transaction = null,
            int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?))
        {
            return dbconn.ExecuteAsync(sql, param, transaction, commandTimeout, commandType);
        }

        #endregion

        /// <summary>
        /// 根据sql获取DataTable
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="paras">参数</param>
        /// <param name="tran">事务</param>
        /// <returns></returns>
        public DataTable LoadDTbySQL(string sql, SqlParameter[] paras = null, IDbTransaction tran = null)
        {
            // XXX: 初步实现，待优化

            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = null;
            try
            {
                if (tran == null)
                {
                    SqlCommand command = dbconn.CreateCommand() as SqlCommand;
                    command.CommandText = sql;
                    command.CommandType = CommandType.Text;
                    if (paras != null && paras.Length > 0)
                    {
                        command.Parameters.AddRange(paras);
                    }
                    adapter = new SqlDataAdapter(command);
                    adapter.Fill(dataTable);
                }
                else
                {
                    if (tran.Connection.State != ConnectionState.Open)
                    {
                        tran.Connection.Open();
                    }
                    SqlCommand command = tran.Connection.CreateCommand() as SqlCommand;
                    command.Transaction = tran as SqlTransaction;
                    command.CommandText = sql;
                    command.CommandType = CommandType.Text;
                    if (paras != null && paras.Length > 0)
                    {
                        command.Parameters.AddRange(paras);
                    }
                    adapter = new SqlDataAdapter(command);
                    adapter.Fill(dataTable);
                }

            }
            catch (OleDbException ex)
            {
                Console.WriteLine("[Repository]--> public DataTable LoadDTbySQL()" + ex.Message);
            }
            finally
            {
                adapter.Dispose();
            }
            return dataTable;
        }
    }
}
